package com.powernode.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.reggie.common.BaseContext;
import com.powernode.reggie.common.R;
import com.powernode.reggie.entity.ShoppingCart;
import com.powernode.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.module.Configuration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:  购物车
 * @author: www
 * @time: 2023/3/16 16:53
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        log.info("购物车 {}",shoppingCart);
        //设置用户id，指定当前购物车是哪个用户的
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);//设置到当前购物车中

        //查询菜品或套餐是否在购物车中，菜品提交的是dishid 套餐时setmealid
        Long dishId = shoppingCart.getDishId();//查询dishid 是否存在进行判断

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId != null) {
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);//查菜品id
        }else{
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);//查套餐id
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果存在，在原来基础上数量加一
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //不存在，添加到购物车，数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());//设置入库时间
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;//走这个分支说明 cartServiceOne 没有赋值，所以统一下，方便返回
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车 条件查询， 条件是用户id
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        log.info("查看购物车");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());//根据用户id查
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCarts);
    }

    /**
     * 清空购物车  根据userid删除
     * @return
     */
    @DeleteMapping("/clean")
    public  R<String> clean(){

        log.info("清空购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功");
    }

}
